﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using TwitterClient2.Model;
using System.Drawing;
using System.IO;
using NTwitter;
using TwitterClient2.Controller;

namespace TwitterClient2.Components
{
    public class PanelUser : Panel
    {
        private PictureBox pictureBoxUserPicture;
        private TwitterClientModel model = TwitterClientModel.getInstance();
        private TwitterClientController controller = TwitterClientController.getInstance();
        private Label labelName;
        private Label labelScreenName;
        private Label labelId;
        private Label labelLocation;
        private Label labelHomepage;
        private Label labelDiscription;
        private Label labelFriends;
        private Label labelFollowers;
        private Label labelStatus;
        private Label labelIdValue;
        private Label labelNameValue;
        private Label labelScreenNameValue;
        private Label labelLocationValue;
        private Label labelHomepageValue;
        private Label labelFriendsValue;
        private Label labelFollowersValue;
        private Label labelDiscriptionValue;
        private Label labelStatusValue;
        private Button buttonFollowUnFollow;
        private PanelNavigation panelNavigation = new PanelNavigation("User Test", PanelNavigation.PANEL_HOME);
        private bool follow = false;
        private User user;

        public PanelUser()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.pictureBoxUserPicture = new System.Windows.Forms.PictureBox();
            this.labelName = new System.Windows.Forms.Label();
            this.labelScreenName = new System.Windows.Forms.Label();
            this.labelId = new System.Windows.Forms.Label();
            this.labelLocation = new System.Windows.Forms.Label();
            this.labelHomepage = new System.Windows.Forms.Label();
            this.labelDiscription = new System.Windows.Forms.Label();
            this.labelFriends = new System.Windows.Forms.Label();
            this.labelFollowers = new System.Windows.Forms.Label();
            this.labelStatus = new System.Windows.Forms.Label();
            this.labelIdValue = new System.Windows.Forms.Label();
            this.labelNameValue = new System.Windows.Forms.Label();
            this.labelScreenNameValue = new System.Windows.Forms.Label();
            this.labelLocationValue = new System.Windows.Forms.Label();
            this.labelHomepageValue = new System.Windows.Forms.Label();
            this.labelFriendsValue = new System.Windows.Forms.Label();
            this.labelFollowersValue = new System.Windows.Forms.Label();
            this.labelDiscriptionValue = new System.Windows.Forms.Label();
            this.labelStatusValue = new System.Windows.Forms.Label();
            this.buttonFollowUnFollow = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // pictureBoxUserPicture
            // 
            this.pictureBoxUserPicture.Location = new System.Drawing.Point(20, 20);
            this.pictureBoxUserPicture.Name = "pictureBoxUserPicture";
            this.pictureBoxUserPicture.Size = new System.Drawing.Size(73, 73);
            //
            // buttonFollowUnfollow
            //
            this.buttonFollowUnFollow.Size = new Size(80, 20);
            this.buttonFollowUnFollow.Location = new Point(10, 83);
            this.buttonFollowUnFollow.Click += new EventHandler(buttonFollowUnFollowClick);
            // 
            // labelName
            // 
            this.labelName.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
            this.labelName.Location = new System.Drawing.Point(103, 71);
            this.labelName.Name = "labelName";
            this.labelName.Size = new System.Drawing.Size(100, 15);
            this.labelName.Text = "Name:";
            // 
            // labelScreenName
            // 
            this.labelScreenName.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
            this.labelScreenName.Location = new System.Drawing.Point(103, 46);
            this.labelScreenName.Name = "labelScreenName";
            this.labelScreenName.Size = new System.Drawing.Size(100, 15);
            this.labelScreenName.Text = "Screenname:";
            // 
            // labelId
            // 
            this.labelId.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
            this.labelId.Location = new System.Drawing.Point(103, 20);
            this.labelId.Name = "labelId";
            this.labelId.Size = new System.Drawing.Size(100, 15);
            this.labelId.Text = "User ID:";
            // 
            // labelLocation
            // 
            this.labelLocation.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
            this.labelLocation.Location = new System.Drawing.Point(10, 110);
            this.labelLocation.Name = "labelLocation";
            this.labelLocation.Size = new System.Drawing.Size(100, 15);
            this.labelLocation.Text = "Location:";
            // 
            // labelHomepage
            // 
            this.labelHomepage.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
            this.labelHomepage.Location = new System.Drawing.Point(110, 110);
            this.labelHomepage.Name = "labelHomepage";
            this.labelHomepage.Size = new System.Drawing.Size(100, 15);
            this.labelHomepage.Text = "Homepage";
            // 
            // labelDiscription
            // 
            this.labelDiscription.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
            this.labelDiscription.Location = new System.Drawing.Point(10, 160);
            this.labelDiscription.Name = "labelDiscription";
            this.labelDiscription.Size = new System.Drawing.Size(100, 15);
            this.labelDiscription.Text = "Discription:";
            // 
            // labelFriends
            // 
            this.labelFriends.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
            this.labelFriends.Location = new System.Drawing.Point(10, 135);
            this.labelFriends.Name = "labelFriends";
            this.labelFriends.Size = new System.Drawing.Size(100, 15);
            this.labelFriends.Text = "Friends:";
            // 
            // labelFollowers
            // 
            this.labelFollowers.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
            this.labelFollowers.Location = new System.Drawing.Point(110, 135);
            this.labelFollowers.Name = "labelFollowers";
            this.labelFollowers.Size = new System.Drawing.Size(100, 15);
            this.labelFollowers.Text = "Followers:";
            // 
            // labelStatus
            // 
            this.labelStatus.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
            this.labelStatus.Location = new System.Drawing.Point(10, 215);
            this.labelStatus.Name = "labelStatus";
            this.labelStatus.Size = new System.Drawing.Size(100, 15);
            this.labelStatus.Text = "Status:";
            // 
            // labelIdValue
            // 
            this.labelIdValue.Font = new System.Drawing.Font("Tahoma", 7F, System.Drawing.FontStyle.Regular);
            this.labelIdValue.Location = new System.Drawing.Point(106, 36);
            this.labelIdValue.Name = "labelIdValue";
            this.labelIdValue.Size = new System.Drawing.Size(100, 14);
            this.labelIdValue.Text = "label1";
            // 
            // labelNameValue
            // 
            this.labelNameValue.Font = new System.Drawing.Font("Tahoma", 7F, System.Drawing.FontStyle.Regular);
            this.labelNameValue.Location = new System.Drawing.Point(106, 86);
            this.labelNameValue.Name = "labelNameValue";
            this.labelNameValue.Size = new System.Drawing.Size(100, 15);
            this.labelNameValue.Text = "label2";
            // 
            // labelScreenNameValue
            // 
            this.labelScreenNameValue.Font = new System.Drawing.Font("Tahoma", 7F, System.Drawing.FontStyle.Regular);
            this.labelScreenNameValue.Location = new System.Drawing.Point(106, 61);
            this.labelScreenNameValue.Name = "labelScreenNameValue";
            this.labelScreenNameValue.Size = new System.Drawing.Size(100, 14);
            this.labelScreenNameValue.Text = "label3";
            // 
            // labelLocationValue
            // 
            this.labelLocationValue.Font = new System.Drawing.Font("Tahoma", 7F, System.Drawing.FontStyle.Regular);
            this.labelLocationValue.Location = new System.Drawing.Point(113, 125);
            this.labelLocationValue.Name = "labelLocationValue";
            this.labelLocationValue.Size = new System.Drawing.Size(100, 14);
            this.labelLocationValue.Text = "labelLocationValue";
            // 
            // labelHomepageValue
            // 
            this.labelHomepageValue.Font = new System.Drawing.Font("Tahoma", 7F, System.Drawing.FontStyle.Regular);
            this.labelHomepageValue.Location = new System.Drawing.Point(13, 125);
            this.labelHomepageValue.Name = "labelHomepageValue";
            this.labelHomepageValue.Size = new System.Drawing.Size(100, 14);
            this.labelHomepageValue.Text = "label2";
            // 
            // labelFriendsValue
            // 
            this.labelFriendsValue.Font = new System.Drawing.Font("Tahoma", 7F, System.Drawing.FontStyle.Regular);
            this.labelFriendsValue.Location = new System.Drawing.Point(13, 150);
            this.labelFriendsValue.Name = "labelFriendsValue";
            this.labelFriendsValue.Size = new System.Drawing.Size(100, 14);
            this.labelFriendsValue.Text = "label1";
            // 
            // labelFollowersValue
            // 
            this.labelFollowersValue.Font = new System.Drawing.Font("Tahoma", 7F, System.Drawing.FontStyle.Regular);
            this.labelFollowersValue.Location = new System.Drawing.Point(113, 150);
            this.labelFollowersValue.Name = "labelFollowersValue";
            this.labelFollowersValue.Size = new System.Drawing.Size(100, 14);
            this.labelFollowersValue.Text = "label2";
            // 
            // labelDiscriptionValue
            // 
            this.labelDiscriptionValue.Font = new System.Drawing.Font("Tahoma", 7F, System.Drawing.FontStyle.Regular);
            this.labelDiscriptionValue.Location = new System.Drawing.Point(13, 175);
            this.labelDiscriptionValue.Name = "labelDiscriptionValue";
            this.labelDiscriptionValue.Size = new System.Drawing.Size(220, 50);
            this.labelDiscriptionValue.Text = "label1";
            // 
            // labelStatusValue
            // 
            this.labelStatusValue.Font = new System.Drawing.Font("Tahoma", 7F, System.Drawing.FontStyle.Regular);
            this.labelStatusValue.Location = new System.Drawing.Point(13, 230);
            this.labelStatusValue.Name = "labelStatusValue";
            this.labelStatusValue.Size = new System.Drawing.Size(220, 30);
            this.labelStatusValue.Text = "label2";
            // 
            // PanelUser
            // 
            this.Controls.Add(this.buttonFollowUnFollow);
            this.Controls.Add(this.pictureBoxUserPicture);
            this.Controls.Add(this.labelId);
            this.Controls.Add(this.labelScreenName);
            this.Controls.Add(this.labelName);
            this.Controls.Add(this.labelScreenNameValue);
            this.Controls.Add(this.labelIdValue);
            this.Controls.Add(this.labelNameValue);
            this.Controls.Add(this.labelLocation);
            this.Controls.Add(this.labelDiscription);
            this.Controls.Add(this.labelFollowers);
            this.Controls.Add(this.labelFriends);
            this.Controls.Add(this.labelHomepage);
            this.Controls.Add(this.labelStatus);
            this.Controls.Add(this.labelLocationValue);
            this.Controls.Add(this.labelHomepageValue);
            this.Controls.Add(this.labelDiscriptionValue);
            this.Controls.Add(this.labelStatusValue);
            this.Controls.Add(this.labelFriendsValue);
            this.Controls.Add(this.labelFollowersValue);
            this.Controls.Add(this.panelNavigation);

            this.ResumeLayout(false);

        }

        public void uploadData(User user, byte navigationtype)
        {
            this.user = user;
            this.Controls.Remove(this.panelNavigation);
            this.panelNavigation = new PanelNavigation("User " + user.ScreenName, navigationtype);
            this.panelNavigation.Location = new Point(10, 0);
            this.Controls.Add(this.panelNavigation);
            byte[] imageData = model.downLoadData(user.ProfileImageUrl.ToString()); //DownloadData function from here
            MemoryStream stream = new MemoryStream(imageData);
            Image img = new Bitmap(stream);
            stream.Close();
            this.pictureBoxUserPicture.Image = img;
            if (user.ScreenName != null)
                this.labelScreenNameValue.Text = user.ScreenName;
            else
                this.labelScreenNameValue.Text = "-";
            if (user.Id != -1)
                this.labelIdValue.Text = "" + user.Id;
            else
                this.labelIdValue.Text = "-";
            if (user.Name != null)
                this.labelNameValue.Text = user.Name;
            else
                this.labelNameValue.Text = "-";
            if (user.Name != null)
                this.labelNameValue.Text = user.Name;
            else
                this.labelNameValue.Text = "-";
            if (user.Location != null)
                this.labelLocationValue.Text = user.Location;
            else
                this.labelLocationValue.Text = "-";
            if (user.Website != null)
                this.labelHomepageValue.Text = user.Website.ToString();
            else
                this.labelHomepageValue.Text = "-";
            if (user.FriendsCount != -1)
                this.labelFriendsValue.Text = user.FriendsCount + "";
            else
                this.labelFriendsValue.Text = "-";
            if (user.FollowersCount != -1)
                this.labelFollowersValue.Text = user.FollowersCount + "";
            else
                this.labelFollowersValue.Text = "-";
            if (user.Description != null)
                this.labelDiscriptionValue.Text = user.Description;
            else
                this.labelDiscriptionValue.Text = "-";
            if (user.Status != null)
            {
                if (user.Status.Text != null)
                    this.labelStatusValue.Text = user.Status.Text;
            }
            else
                this.labelStatusValue.Text = "-";

            this.buttonFollowUnFollow.Text = "Follow";
            this.follow = false;
            foreach (User u in this.model.friends)
            {
                if (user.ScreenName.Equals(u.ScreenName))
                {
                    this.buttonFollowUnFollow.Text = "Unfollow";
                    this.follow = true;
                    break;
                }
            }


            this.Refresh();
        }
        private void buttonFollowUnFollowClick(object sender, EventArgs args)
        {
            this.Enabled = false;
            Cursor.Current = Cursors.WaitCursor;
            TwitterClientEvent tEvent = new TwitterClientEvent();
            tEvent.paramList.Add(this.user.ScreenName);
            if (this.follow)
            {
                this.controller.performUserInput(TwitterClientController.UNFOLLOW_USER, tEvent);
                this.buttonFollowUnFollow.Text = "Follow";
                this.follow = true;
            }
            else
            {
                this.controller.performUserInput(TwitterClientController.FOLLOW_USER, tEvent);
                this.buttonFollowUnFollow.Text = "Unfollow";
                this.follow = false;
            }
            Cursor.Current = Cursors.Default;
            this.Enabled = true;
        }
    }


}
